# rules for builds
# contains the compiler and linker rules

# path
VPATH = ../..

# echo cancelation
ECHO =
# @

# the tools rules and flags
AS = $(ECHO)gcc
ASFLAGS = -g -mips32 -EB -mgp32 -mfp32 -ansi -c
#-nostdinc

CPP =
CPPFLAGS =

CC = $(ECHO)gcc
CFLAGS = -g -mips32 -EB -mgp32 -mfp32 -ansi -c
#-nostdinc

LD = $(ECHO)ld
LDFLAGS = -EB -e kernel_Initialise -Map bin/map.txt

DEP = $(ECHO)makedepend
#$(ECHO)gcc
DEPFLAGS = -Y
#-MM
DEPOUT = -f
#>>

DEL = $(ECHO)rm -f

#include all the modules and create the SRC macro
SRC :=
include $(patsubst %,../../%/module.mak,$(MODULES))

# convert source list into obj list
OBJ = $(patsubst %.cpp,%.o,$(filter %.cpp,$(SRC)))
OBJ += $(patsubst %.c,%.o,$(filter %.c,$(SRC)))
OBJ += $(patsubst %.s,%.o,$(filter %.s,$(SRC)))

# include file driectories
INC = $(patsubst %,../../%/inc,$(MODULES))

# rules
.PHONY : clean depend

all : exe/$(BUILDNAME).out

exe/$(BUILDNAME).out: $(OBJ) $(LIB) makefile $(patsubst %,../../%/module.mak,$(MODULES))
	$(LD) $(LFLAGS) $(OBJ) $(LIB) -o $@

%.o : %.cpp
	$(CC) $(CFLAGS) $(patsubst %,-I%,$(INC)) $< -o $@

%.o : %.c
	$(CC) $(CFLAGS) $(patsubst %,-I%,$(INC)) $< -o $@

%.o : %.s
	$(AS) $(ASFLAGS) $(patsubst %,-I%,$(INC)) $< -o $@

depend :
	@echo "# GENERATED FILE!" > depend.mak
	@echo >> depend.mak
	$(DEP) $(DEPFLAGS) $(patsubst %,-I%,$(INC)) $(SRC) $(DEPOUT)depend.mak

clean :
	$(DEL) $(OBJ)
	$(DEL) exe/*
